package com.infore.airsupervise.adapter;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.CompareE;

import java.util.ArrayList;
import java.util.List;

/**
 * 同期对比列表适配器
 * Created by xiaotao on 2017/8/17.
 */

public class CompareAnalyzeAdapter  extends RecyclerView.Adapter<CompareAnalyzeAdapter.ViewHolder>{

    private int compareType ; //对比类型  同比环比
    private List<CompareE> data;
    private String curTime; //参照时间
    private String befTime; //对比时间
    private List<String> xLabel= null ;

    private int [] colors = new int[]{R.color.google_green,R.color.google_yellow,R.color.colorPrimary};

    public CompareAnalyzeAdapter(List<CompareE> data) {

        this.data = data==null ? new ArrayList<CompareE>():data;
    }

    public void setLabelTime(String curTime ,String befTime) {
        this.curTime = curTime;
        this.befTime = befTime;
    }

    public void setCompareType(int compareType) {
        this.compareType = compareType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_compare, parent, false);

        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        CompareE compareE = data.get(position);
        holder.tv_name.setText(compareE.getStationName());
        holder.tv_current.setText(curTime);
        holder.tv_before.setText(befTime);
        holder.label_cur.setBackgroundColor( ContextCompat.getColor(holder.itemView.getContext(), colors[2]));
        holder.label_bef.setBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), colors[compareType]));
        initChart(holder.mChart , data.get(position) ,  position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0: data.size();
    }



     class ViewHolder extends RecyclerView.ViewHolder{
         private TextView tv_name;
         private TextView tv_current;
         private TextView tv_before;
         private BarChart mChart;
         private View label_cur;
         private View label_bef;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_before = (TextView) itemView.findViewById(R.id.tv_label_before);
            tv_current = (TextView) itemView.findViewById(R.id.tv_label_current);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            mChart = (BarChart) itemView.findViewById(R.id.chart);
            label_cur = itemView.findViewById(R.id.iv_label_cur);
            label_bef = itemView.findViewById(R.id.iv_label_before);
        }
    }


    private void initChart(BarChart mChart , CompareE compareE , final int position){
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.getDescription().setEnabled(false);
        mChart.setPinchZoom(false);
        mChart.setScaleYEnabled(false);
        mChart.setDrawGridBackground(false);
        mChart.getLegend().setEnabled(false);
        //右边Y轴
        YAxis rightY = mChart.getAxisRight();
        rightY.setEnabled(false);
        //x轴
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
       // xAxis.setDrawLabels(false); //是否绘制标签
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setCenterAxisLabels(true);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int index = (int) value;
                if (index < 0 || index >= xLabel.size()) {
                    return "";
                }
                double contract = data.get(position).getContrast().get(index);
                return xLabel.get(index)+"//"+contract*100;
            }
        });
        // 左Y轴
        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setDrawAxisLine(false);
        leftAxis.setDrawGridLines(true);
        leftAxis.setDrawLabels(false);
        leftAxis.setGranularityEnabled(true);

        leftAxis.setSpaceTop(15f);//最高位置距离顶部的位置
        setChartData(mChart,compareE);
    }

    private void setChartData(BarChart mChart ,CompareE compareE){

        float groupSpace = 0.24f;
        float barSpace = 0.08f; // x2 DataSet
        float barWidth = 0.3f; // x2 DataSet

        List<BarEntry> curListEntry = new ArrayList<>(); //参照
        List<BarEntry> befListEntry = new ArrayList<>(); //对比
        if(xLabel == null){
            xLabel = new ArrayList<>();
            for(int i = 0 ;i<compareE.getFactor().size() ; i++){
                xLabel.add(compareE.getFactor().get(i));
            }
        }

        for(int i = 0 ; i<compareE.getCurrent().size() ; i++){
            befListEntry.add(new  BarEntry(i,compareE.getPass().get(i).floatValue()));
            curListEntry.add(new BarEntry(i,compareE.getCurrent().get(i).floatValue()));
        }

        BarDataSet curSet , befSet ;
        if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
            befSet = (BarDataSet) mChart.getData().getDataSetByIndex(0);
            curSet = (BarDataSet) mChart.getData().getDataSetByIndex(1);

            befSet.setColor(ContextCompat.getColor(mChart.getContext(),colors[compareType]));
            curSet.setColor(ContextCompat.getColor(mChart.getContext(),colors[2]));
            befSet.setValues(befListEntry);
            curSet.setValues(curListEntry);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        }else {
            befSet = new BarDataSet(befListEntry,"before");
            curSet = new BarDataSet(curListEntry,"current");
            curSet.setValueFormatter(new DefaultValueFormatter(2));
            befSet.setValueFormatter(new DefaultValueFormatter(2));
            befSet.setColor(ContextCompat.getColor(mChart.getContext(),colors[compareType]));
            curSet.setColor(ContextCompat.getColor(mChart.getContext(),colors[2]));
            BarData data = new BarData(befSet,curSet);
            data.setHighlightEnabled(false);
            mChart.setData(data);
        }

        mChart.getBarData().setBarWidth(barWidth);

        // restrict the x-axis range
        mChart.getXAxis().setAxisMinimum(0);
        mChart.getXAxis().setAxisMaximum(0 + mChart.getBarData().getGroupWidth(groupSpace, barSpace) * compareE.getCurrent().size());
        mChart.groupBars(0, groupSpace, barSpace);
        mChart.invalidate();
    }
}
